package com.wxit.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxit.eduservice.entity.EduChapter;
import com.wxit.eduservice.entity.EduVideo;
import com.wxit.eduservice.entity.chapter.ChapterVo;
import com.wxit.eduservice.entity.chapter.VideoVo;
import com.wxit.eduservice.mapper.EduChapterMapper;
import com.wxit.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxit.eduservice.service.EduVideoService;
import com.wxit.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 吴杰
 * @since 2021-11-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    /**
     * 更具课程id查询课程大纲
     * @param courseId
     * @return
     */
    @Override
    public List<ChapterVo> nestedList(String courseId) {
        //获取章节信息
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        List<EduChapter> chapters = baseMapper.selectList(queryWrapper);

        //获取课时信息
        QueryWrapper<EduVideo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<EduVideo> videos = eduVideoService.list(queryWrapper1);

        List<ChapterVo> chapterVos = new ArrayList<>();
        //填充章节vo数据
        for (EduChapter chapter : chapters) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVos.add(chapterVo);
            List<VideoVo> videoVos = new ArrayList<>();
            for (EduVideo video : videos) {
                if (chapter.getId().equals(video.getChapterId())){
                    //创建课时对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVos.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVos);
        }
        //填充课时vo数据
        return chapterVos;
    }

    /**
     *
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);

        int count = eduVideoService.count(wrapper);
        if (count > 0){ //不删除
            throw  new GuliException(20001,"不能删除");
        } else { //删除
            int result = baseMapper.deleteById(chapterId);
            return  result > 0;
        }
    }

    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
